﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

using Perseus.Xml;

using Hetymine.Executioner.Controls;

namespace Hetymine.Executioner.Commands {
    public class UnknownCommand : ICommand {
        private static UnknownCommandConfigurator _Configurator;
        private XElement _Element;

        public UnknownCommand() {
            this.Type = "Unknown";
            this.Name = string.Empty;
            this.CallStrings = new List<string>();
        }

        public string Type { get; set; }

        #region ICommand Members
        public string Name { get; set; }
        public bool IsEmpty { get { return false; } }

        public void Load(XElement element) {
            this._Element = element;

            this.Type = element.Name.LocalName;

            this.Name = element.Element("Name").ValueOrEmpty();
            
            var callStrings = from s in element.Elements("CallString")
                              select s;

            foreach (string s in callStrings) {
                this.CallStrings.Add(s);
            }
        }
        public XElement Save() {
            return this._Element;
        }

        public List<string> CallStrings { get; set; }
        public ExecutionResult Execute(string parameters) {
            return ExecutionResult.None;
        }

        public ICommandConfigurator<ICommand> Configurator {
            get {
                if (UnknownCommand._Configurator == null) {
                    UnknownCommand._Configurator = new UnknownCommandConfigurator();
                }
                UnknownCommand._Configurator.InitializeConfig(this);
                return UnknownCommand._Configurator;
            }
        }

        public object Clone() {
            UnknownCommand uc = (UnknownCommand)this.MemberwiseClone();

            uc._Element = XElement.Parse(this._Element.ToString());

            uc.CallStrings = new List<string>();
            foreach (string cs in this.CallStrings) {
                uc.CallStrings.Add(cs);
            }

            return uc;
        }
        #endregion
    }
}
